package cn.wzptsoft.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.pojo.Category;
import cn.wzptsoft.mall.pojo.result.Result;
import cn.wzptsoft.mall.pojo.vo.CategoryInfoVo;
import cn.wzptsoft.mall.pojo.vo.CategoryVo;
import cn.wzptsoft.mall.pojo.vo.saveCategoryVo;
import cn.wzptsoft.mall.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@Tag(name = "商品相关接口")
@RequiredArgsConstructor
public class CategoryController {

    private final ICategoryService iCategoryService;

    @GetMapping("/all")
    @Operation(summary = "查找所有商品分类")
    public Result<CategoryVo> listAllCategory(){
        CategoryVo vo= iCategoryService.listAllCategor();
        return Result.ok(vo);

    }

    @GetMapping("/grid")
    @Operation(summary = "查找根分类")
    public Result<List<CategoryInfoVo>> listRootCategor(){
        List<CategoryInfoVo> vo= iCategoryService.listRootCategor();
        return Result.ok(vo);

    }

    @PutMapping
    @Operation(summary = "新增商品")
    @PreAuthorize("hasRole('admin')")
    public Result saveCategory(@RequestBody saveCategoryVo saveCategoryVo){
        iCategoryService.add(BeanUtil.copyProperties(saveCategoryVo, Category.class));
        return Result.ok();

    }

    @PutMapping("/update")
    @Operation(summary = "更新商品")
    @PreAuthorize("hasRole('admin')")
    public Result updateCategory(@RequestBody saveCategoryVo saveCategoryVo){

        iCategoryService.updateCategory(BeanUtil.copyProperties(saveCategoryVo, Category.class));

        return Result.ok();

    }

    @DeleteMapping
    @Operation(summary = "删除商品")
    @PreAuthorize("hasRole('admin')")
    public Result delete(Integer id){
        iCategoryService.removeById(id);
        return Result.ok();
    }


}
